<?php

namespace App\Http\Controllers\Admin;

use App\Repository\Shop\SuppilerRepository as model;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;
use App\Repository\Product\GoodsRepository as Goods;
use App\Repository\Shop\DistGoodsSetRepository as DistGoods;
use Illuminate\Support\Facades\DB;

/**
 * @OA\Tag(
 *     name="后台供应商模块",
 *     description="供应商模块",
 * )
 */
class SuppilerController extends BaseController
{
  private $repoist;
  private $goodsModel;
  private $distGoods;
  public function __construct(model $model, Goods $Goods, DistGoods $DistGoods)
  {
    $this->repoist = $model;
    $this->goodsModel = $Goods;
    $this->distGoods = $DistGoods;
  }
  public function store(Request $request)
  {
    return $this->repoist->add($request);
  }
  public function index(Request $request)
  {
    $where = [];
    if (!empty($request->get('phone'))) {
      $where['where']['phone'] = $request->get('phone');
    }
    if (!empty($request->get('name'))) {
      $where['like'] = ['name' => $request->get('name')];
    }
    if (!empty($request->get('number'))) {
      $where['like'] = ['name' => $request->get('number')];
    }
    $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
    $where['limit']['number'] = $request->get('pageSize');
    $where['where'][] = ['shop_id', '=', $request->user->id];
    $data =  $this->repoist->index($where);
    return $data;
  }
  public function save(Request $request)
  {
    if (!$request->get('id')) {
      return $this->returnMsg(1, '请传递id参数');
    }
    $where = ['id' => $request->get('id')];
    return $this->repoist->edit($where, $request);
  }
  public function  show(Request $request, $id)
  {
    $where = [
      'id' => $id,
      'shop_id' => $request->user->shop_id,
    ];
    //查询一条数据
    $return = $this->repoist->getOne($where);
    if (!empty($return)) {
      $return = usfirstArr($return);
    }
    return $this->returnMsg(200, '查询成功', $return);
  }

  public function listss(Request $request)
  {
    $where = [];
    if (!empty($request->get('name'))) {
      $where['where'] = ['name' => $request->get('name')];
    }

    $return = $this->repoist->lists($where);
    return $return;
  }

  public function destroy(Request $request, $id)
  {
    if (!$id) {
      return $this->returnMsg(1, '请传递参数');
    }
    $where = [
      'id' => $id,
      'shop_id' => $request->user->shop_id,
    ];
    return $this->repoist->edit($where, ['status' => 1]);
  }
  // 供应商的商品
  public function suppilerGoods_back(Request $request, $id)
  {
    // var_dump($request->all());
    $where = [
      'id' => $id,
    ];
    if ($request->user->role_id != 1) {
      $where = [
        'id' => $id,
        'shop_id' => $request->user->shop_id,
      ];
    }
    $res = $this->repoist->getOne($where);
    if (empty($res)) {
      return $this->returnMsg(1, '未查询到供应商的商品');
    }
    // 根据供应商的id  获取他的产品
    $whereGoods['where']['goods.shop_id'] = $res->supplier_id;
    $whereGoods['where']['goods.status'] = 0;
    if (!empty($request->get('goods_name'))) {
      $whereGoods['like']['goods.name'] = $request->get('goods_name');
    }
    if (!empty($request->get('suppiler_name'))) {
      $whereGoods['like']['goods.shop_name'] = $request->get('suppiler_name');
    }
    $whereGoods['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
    $whereGoods['limit']['number'] = $request->get('pageSize');
    // $whereGoods['select'] = ['id', 'name', 'created_at', 'main_pic', 'goods_code', 'settle_require', 'shop_id', 'shop_name', 'goods_point'];
    $whereGoods['join'][] = [
      'table' => 'admin_users',
      'left'  => 'id',
      'right' => 'goods.shop_id'
    ];
    // $whereGoods['join'][] = [
    //   'table' => 'dist_goods_set',
    //   'left'  => 'goods_id',
    //   'right' => 'goods.id'
    // ];
    // $whereGoods['where'][] = ['dist_goods_set.dist_type', '>', 0];
    $whereGoods['select'] = ['admin_users.user_name', 'goods.id', 'goods.name', 'goods.created_at', 'goods.main_pic', 'goods.goods_code', 'goods.settle_require', 'goods.shop_id', 'goods.shop_name', 'goods.goods_point', 'goods.down_str', 'dist_goods_set.dist_type', 'dist_goods_set.dist_shop_id'];
    // var_dump($whereGoods, 1111111111);
    $returnData =  $this->goodsModel->index($whereGoods);
    $shop_id = $request->user->shop_id;
    if (!empty($returnData['data'])) {
      foreach ($returnData['data'] as $key => &$v) {
        $v['is_down'] = true;
        if (strstr($v['downStr'], '.' . $shop_id . '.')) {
          $v['is_down'] = false;
        }
      }
    }
    return $returnData;;
  }
  public function suppilerGoods(Request $request, $id)
  {
    $where = [
      'id' => $id,
    ];
    if ($request->user->role_id != 1) {
      $where = [
        'id' => $id,
        'shop_id' => $request->user->shop_id,
      ];
    }

    $res = $this->repoist->getOne($where);
    if (empty($res)) {
      return $this->returnMsg(1, '未查询到供应商的商品');
    }
    $whereGoods['where']['dist_goods_set.shop_id'] = $res->supplier_id;
    $whereGoods['where'][] = [
      'dist_goods_set.dist_type', '>=', 1,
    ];
    if (!empty($request->get('goods_name'))) {
      $whereGoods['like']['goods.name'] = $request->get('goods_name');
    }
    if (!empty($request->get('suppiler_name'))) {
      $whereGoods['like']['admin_users.user_name'] = $request->get('suppiler_name');
    }
    $whereGoods['join'][] = [
      'table' => 'goods',
      'left'  => 'id',
      'right' => 'dist_goods_set.goods_id'
    ];
    $whereGoods['join'][] = [
      'table' => 'admin_users',
      'left'  => 'id',
      'right' => 'dist_goods_set.shop_id'
    ];
    $whereGoods['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
    // $whereGoods['limit']['number'] = $request->get('pageSize');
    $whereGoods['limit']['number'] = 10000;
    $whereGoods['select'] = ['admin_users.user_name', 'goods.id', 'goods.name', 'goods.created_at', 'goods.main_pic', 'goods.goods_code', 'goods.settle_require', 'goods.shop_id', 'goods.shop_name', 'goods.goods_point', 'goods.down_str', 'dist_goods_set.dist_type', 'dist_goods_set.goods_id as dist_goods_set_goods_id', 'dist_goods_set.dist_shop_id'];
    // DB::connection()->enableQueryLog();
    $returnData = $this->distGoods->index($whereGoods);
    // $log = DB::getQueryLog();
    // var_dump($log);
    $shop_id = $request->user->shop_id;
    if (!empty($returnData['data'])) {
      foreach ($returnData['data'] as $key => &$v) {
        // 所有可见
        // if ($v['dist_type'] == 1) {
        //   continue;
        // }
        // 部分可见
        if ($v['dist_type'] == 2 && !empty($v['dist_shop_id'])) {
          // 用户id 是否存在 如果存在则可使用
          $isTrue = strpos($v['dist_shop_id'], $shop_id . ',');
          // var_dump($isTrue, 222222222);
          if (isset($isTrue) && $isTrue !== 0) {
            $isTrueTwo = strpos($v['dist_shop_id'], ',' . $shop_id . ',');
            if (empty($isTrueTwo)) {
              unset($returnData['data'][$key]);
              continue;
            }
          }
        }
        // 部分不可见
        if ($v['dist_type'] == 3 && !empty($v['dist_shop_id'])) {
          // var_dump($shop_id, 11111111);
          // var_dump($v['dist_shop_id']);
          $isTrue = strpos($v['dist_shop_id'], $shop_id . ',');
          // var_dump($isTrue, 11111);
          if (isset($isTrue) && $isTrue !== false) {
            // var_dump();
            unset($returnData['data'][$key]);
            continue;
            // $isTrueTwo = strpos($v['dist_shop_id'], ',' . $shop_id . ',');
            // if (!empty($isTrueTwo)) {

            // }
          }
        }
        $v['is_down'] = true;
        if (strstr($v['down_str'], '.' . $shop_id . '.')) {
          $v['is_down'] = false;
        }
      }
    }
    // 防止报错  将 data数据处理
    $datas = [];
    if (!empty($returnData['data'])) {
      foreach ($returnData['data'] as $key => $v) {
        $datas[] = $v;
      }
      $returnData['data'] = $datas;
    }
    // $log = DB::getQueryLog();
    // var_dump($log);
    return $returnData;
  }
  public function addSuppilerGoods(Request $request)
  {
    $params = $request->all();
    if (empty($params['shopId'])) {
      $params['shopId'] = $params['shop_id'];
    }
    // 验证是否是供应商
    $where = [
      'supplier_id' => $params['shop_id'],
      'shop_id' => $request->user->shop_id
    ];
    $params['shop_id'] = $request->user->shop_id;
    $params['goods_id'] = $params['id'];
    $isTrue = $this->repoist->getOne($where);

    if (!$isTrue) {
      return $this->returnMsg(1, '未查询到该您的供应商');
    }
    $params['shop_name'] = $isTrue->name;
    $params['supplier_name'] = $isTrue->supplier_name;
    return $this->goodsModel->addSuppilerGoods($params);
    // var_dump($params);
    // 将获取到供应商的商品存入到商品中  这个地方 需要添加处理 shop_str
    // 订单查询的时候 也需要将 shop_str 存入进去 
    // 自己的商品下单 shop_id 查询的 操作权限是不一样的 自己的单 可以修改产品信息
    // 分销商的订单 shop_str like %.shop_id.%  shop_str 不能加自己的shop_id了 收费的话 直接根据字符串收费就行  shop_name_str 代理商名的字符串 
    // 生成一个拆单 拆单了 第一次提交的信息是有的
  }
  public function distGoods(Request $request, $id)
  {
    $where = [
      'id' => $id,
    ];
    if ($request->user->role_id != 1) {
      $where = [
        'id' => $id,
        'supplier_id' => $request->user->shop_id,
      ];
    }
    $res = $this->repoist->getOne($where);
    if (empty($res)) {
      return $this->returnMsg(1, '未查询到供应商的商品');
    }
    // 根据供应商的id  获取他的产品
    $whereGoods['where']['goods.shop_id'] = $res->shop_id;
    $whereGoods['where']['goods.sup_shop_id'] = $request->user->shop_id;
    $whereGoods['where']['goods.status'] = 0;
    if (!empty($request->get('goods_name'))) {
      $whereGoods['like']['goods.name'] = $request->get('goods_name');
    }
    if (!empty($request->get('suppiler_name'))) {
      $whereGoods['like']['goods.shop_name'] = $request->get('suppiler_name');
    }
    $whereGoods['limit']['start'] = !empty(($request->get('pageNum') - 1) * $request->get('pageSize')) ? ($request->get('pageNum') - 1) * $request->get('pageSize') : 0;
    $whereGoods['limit']['number'] = !empty($request->get('pageSize')) ? $request->get('pageSize') : 100;
    // $whereGoods['select'] = ['id', 'name', 'created_at', 'main_pic', 'goods_code', 'settle_require', 'shop_id', 'shop_name', 'goods_point'];
    $whereGoods['join'][] = [
      'table' => 'admin_users',
      'left'  => 'id',
      'right' => 'goods.shop_id'
    ];
    // $whereGoods['join'][] = [
    //   'table' => 'dist_goods_set',
    //   'left'  => 'goods_id',
    //   'right' => 'goods.id'
    // ];
    // $whereGoods['where'][] = ['dist_goods_set.dist_type', '>', 0];
    $whereGoods['select'] = ['admin_users.user_name', 'goods.id', 'goods.name', 'goods.created_at', 'goods.main_pic', 'goods.goods_code', 'goods.settle_require', 'goods.shop_id', 'goods.shop_name', 'goods.goods_point', 'goods.down_str'];
    // var_dump($whereGoods, 1111111111);
    $returnData =  $this->goodsModel->index($whereGoods);
    $shop_id = $res->shop_id;
    if (!empty($returnData['data'])) {
      foreach ($returnData['data'] as $key => &$v) {
        $v['is_down'] = false;
        // if (strstr($v['downStr'], '.' . $shop_id . '.')) {
        //   $v['is_down'] = false;
        // }
      }
    }
    return $returnData;
  }
}
